package com.lindroy.androidplayer.ui.login

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.elvishew.xlog.XLog
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.databinding.FragmentRegisterBinding
import com.lindroy.androidplayer.logic.network.base.http.observeResponse
import com.lindroy.androidplayer.logic.network.base.http.observeResult
import com.lindroy.androidplayer.logic.network.base.http.observeState
import com.lindroy.androidplayer.utils.saveLoginInfo
import com.lindroy.androidplayer.viewmodels.RegisterViewModel
import com.lindroy.lib_utils.extensions.shortToast

/**
 * @author Lin
 * @date 2022/1/28
 * @function
 */
class RegisterFragment : BaseLazyFragment<FragmentRegisterBinding>() {

    private val viewModel by viewModels<RegisterViewModel>()

    override fun FragmentRegisterBinding.initView(savedInstanceState: Bundle?) {
        vm = viewModel
    }

    override fun FragmentRegisterBinding.initListener() {
        registerLoadingDialogListener()
        btnRegister.setOnClickListener { findNavController().navigateUp() }
        viewModel.registerLiveData.observeState(viewLifecycleOwner){
            onSuccess {
                saveLoginInfo(it)
                shortToast(R.string.register_success)
                activityNavController.popBackStack(R.id.loginFragment,true)
            }
        }
        viewModel.toLoginLiveData.observe(viewLifecycleOwner){
            findNavController().navigateUp()
        }
        viewModel.registerIllegalMessage.observe(viewLifecycleOwner){
            shortToast(it)
        }
    }

    override fun lazyLoad() {
    }
}